package com.hqd.jjvm.rtda.heap.ref;

import com.hqd.jjvm.rtda.heap.JClass;
import com.hqd.jjvm.rtda.heap.JField;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
public class JMemberRef extends JSymRef {
	private String name;
	private String descriptor;

	public JMemberRef(String className, JClass jClass, String name, String descriptor) {
		super(className, jClass);
		this.name = name;
		this.descriptor = descriptor;
	}

	public JMemberRef(JClass refJClass, String className) throws ClassNotFoundException {
		super(refJClass, className);
	}

	public boolean isAccessibleTo(JClass jClassRef, JField jf) {
		if (jf.isPublic())
			return true;
		if (jf.isProtected()) {
			if (jClassRef == this.getJClass() || jClassRef.isSubClassOf(this.getJClass()))
				return true;
		}
		if (!jf.isPrivate())
			return jClassRef.getPackageName() == this.getJClass().getPackageName();
		return jClassRef == this.getJClass();
	}
}
